#include "bodyprotographicsview.h"

#include <QGraphicsPixmapItem>
#include <QGraphicsEllipseItem>
#include <QPen>
#include <QMouseEvent>

const QPen BodyProtoGraphicsView::normalPen = QPen(QBrush(Qt::green), 2);
const QPen BodyProtoGraphicsView::activedPen = QPen(QBrush(Qt::magenta), 3);

const char *BodyProtoGraphicsView::parts[] = {"HEAD", "CHEST", "ABDOMEN", "SPINE", "PELVIS", "UPPER EXTREMITY", "LOWER EXTREMITY"};

BodyProtoGraphicsView::BodyProtoGraphicsView(QWidget *parent) :
    scene(new QGraphicsScene(this)),
    currentItem(0),
    QGraphicsView(parent)
{
    setBackgroundBrush(QBrush(Qt::black));
    initScene();
}

void BodyProtoGraphicsView::initScene()
{
    setScene(scene);
    setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    createItems();
}

void BodyProtoGraphicsView::createItems()
{
    pixmapItem = scene->addPixmap(QPixmap(":/png/body.png"));
    headItem = scene->addEllipse(95, 6, 91, 60, normalPen);
    spineItem = scene->addEllipse(116, 66, 44, 122, normalPen);
    chestItem = scene->addEllipse(160, 92, 48, 100, normalPen);
    abdomenItem = scene->addEllipse(66, 188, 142, 36, normalPen);
    pelvisItem = scene->addEllipse(60, 224, 156, 42, normalPen);
    upperExtremityItem = scene->addEllipse(18, 90, 50, 180, normalPen);
    lowerExtremityItem = scene->addEllipse(74, 264, 52, 184, normalPen);
}

QString BodyProtoGraphicsView::currentBodyPart() const
{
    if (currentItem == headItem) {
        return parts[BP_Head];
    } else if (currentItem == spineItem) {
        return parts[BP_Spine];
    } else if (currentItem == chestItem) {
        return parts[BP_Chest];
    } else if (currentItem == abdomenItem) {
        return parts[BP_Abdomen];
    } else if (currentItem == pelvisItem) {
        return parts[BP_Pelvis];
    } else if (currentItem == upperExtremityItem) {
        return parts[BP_UpperExtremity];
    } else if (currentItem == lowerExtremityItem) {
        return parts[BP_LowerExtremity];
    } else {
        return "";
    }
}

void BodyProtoGraphicsView::setBodyPart(BodyPart part)
{
    if (currentItem) currentItem->setPen(normalPen);
    switch (part) {
    case BP_Head:
        currentItem = headItem;
        emit bodyPartChanged(parts[BP_Head]);
        break;
    case BP_Spine:
        currentItem = spineItem;
        emit bodyPartChanged(parts[BP_Spine]);
        break;
    case BP_Chest:
        currentItem = chestItem;
        emit bodyPartChanged(parts[BP_Chest]);
        break;
    case BP_Abdomen:
        currentItem = abdomenItem;
        emit bodyPartChanged(parts[BP_Abdomen]);
        break;
    case BP_Pelvis:
        currentItem = pelvisItem;
        emit bodyPartChanged(parts[BP_Pelvis]);
        break;
    case BP_UpperExtremity:
        currentItem = upperExtremityItem;
        emit bodyPartChanged(parts[BP_UpperExtremity]);
        break;
    case BP_LowerExtremity:
        currentItem = lowerExtremityItem;
        emit bodyPartChanged(parts[BP_LowerExtremity]);
        break;
    default:
        currentItem = 0;
    }

    if (currentItem) currentItem->setPen(activedPen);
}

void BodyProtoGraphicsView::mousePressEvent(QMouseEvent *e)
{
    QGraphicsItem *item = itemAt(e->pos());
    if (currentItem) currentItem->setPen(normalPen);

    if (item == headItem) {
        currentItem = headItem;
        emit bodyPartChanged(parts[BP_Head]);
    } else if (item == spineItem) {
        currentItem = spineItem;
        emit bodyPartChanged(parts[BP_Spine]);
    } else if (item == chestItem) {
        currentItem = chestItem;
        emit bodyPartChanged(parts[BP_Chest]);
    } else if (item == abdomenItem) {
        currentItem = abdomenItem;
        emit bodyPartChanged(parts[BP_Abdomen]);
    } else if (item == pelvisItem) {
        currentItem = pelvisItem;
        emit bodyPartChanged(parts[BP_Pelvis]);
    } else if (item == upperExtremityItem) {
        currentItem = upperExtremityItem;
        emit bodyPartChanged(parts[BP_UpperExtremity]);
    } else if (item == lowerExtremityItem) {
        currentItem = lowerExtremityItem;
        emit bodyPartChanged(parts[BP_LowerExtremity]);
    }

    if (currentItem) currentItem->setPen(activedPen);
    QGraphicsView::mousePressEvent(e);
}
